package HP.p0828;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.Scanner;

public class Test {

    public static void main(String[] args) throws Exception {
        Test t = new Test();
        t.add();
        // t.update();
        //t.delete();
    }

    public void add() throws Exception {
        Scanner input = new Scanner(System.in);
        System.out.print("请输入编号");
        String n = input.nextLine();
        System.out.print("请输入姓名");
        String name = input.nextLine();
        System.out.print("请输入密码");
        String pwd = input.nextLine();

        // 1、加载驱动
        Class.forName("oracle.jdbc.driver.OracleDriver");

        // 2 定义连接字符串
        String url = "jdbc:oracle:thin:localhost:1521:orcl";
        String uname = "scott";
        String upwd = "tiger";

        // 3 创建Connection连接
        Connection conn = DriverManager.getConnection(url, uname, upwd);

        // 4 创建SQL语句
        String sql = "insert into userinfo values(" + n + ",'" + name + "','" + pwd + "')";

        // 5 创建Statement(发送sql语句到数据库中)
        Statement st = conn.createStatement();

        // 6 发送并执行
        int r = st.executeUpdate(sql);

        if (r > 0) {
            System.out.println("保存成功");
        } else {
            System.out.println("保存失败");
        }
        // 7 关闭连接
        st.close();
        conn.close();
    }

    public void update() throws Exception {

        // 1、加载驱动
        Class.forName("oracle.jdbc.driver.OracleDriver");

        // 2 定义连接字符串
        String url = "jdbc:oracle:thin:localhost:1521:orcl";
        String uname = "scott";
        String upwd = "tiger";

        // 3 创建Connection连接
        Connection conn = DriverManager.getConnection(url, uname, upwd);

        // 4 创建SQL语句
        String sql = "update userinfo set userpwd='666' where userid=1 ";

        // 5 创建Statement(发送sql语句到数据库中)
        Statement st = conn.createStatement();

        // 6 发送并执行
        int r = st.executeUpdate(sql);

        if (r > 0) {
            System.out.println("修改成功");
        } else {
            System.out.println("修改失败");
        }
        // 7 关闭连接
        st.close();
        conn.close();
    }

    public void delete() throws Exception {

        // 1、加载驱动
        Class.forName("oracle.jdbc.driver.OracleDriver");

        // 2 定义连接字符串
        String url = "jdbc:oracle:thin:localhost:1521:orcl";
        String uname = "scott";
        String upwd = "tiger";

        // 3 创建Connection连接
        Connection conn = DriverManager.getConnection(url, uname, upwd);

        // 4 创建SQL语句
        String sql = "delete from Userinfo where userid=3";

        // 5 创建Statement(发送sql语句到数据库中)
        Statement st = conn.createStatement();

        // 6 发送并执行
        int r = st.executeUpdate(sql);

        if (r > 0) {
            System.out.println("删除成功");
        } else {
            System.out.println("删除失败");
        }
        // 7 关闭连接
        st.close();
        conn.close();
    }
}
